import { defineConfig } from 'vite'
import path from 'path'
import tailwindcss from 'tailwindcss'
import react from '@vitejs/plugin-react'
import autoprefixer from 'autoprefixer'
import { viteMockServe } from 'vite-plugin-mock'
import legacy from '@vitejs/plugin-legacy'
import babel from 'vite-plugin-babel'
import { minifyHtml, injectHtml } from 'vite-plugin-html'

export default defineConfig(({ command, mode }) => {
  console.log('process.mode', mode)
  const publicPath = mode === 'dev' ? '/crm' : '/'
  return {
    base: publicPath,
    css: {
      // 进行 PostCSS 配置
      postcss: {
        plugins: [tailwindcss(), autoprefixer()],
      },
    },
    optimizeDeps: {},
    plugins: [
      react(),
      viteMockServe({
        // default
        mockPath: './src/mock/',
        supportTs: true,
        localEnabled: true,
      }),
      babel(),
      legacy({
        targets: ['chrome 52'],
        additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
        renderLegacyChunks: true,
        modernPolyfills: true,
        polyfills: [
          'es.symbol',
          'es.array.filter',
          'es.promise',
          'es.promise.finally',
          'es/map',
          'es/set',
          'es.array.for-each',
          'es.object.define-properties',
          'es.object.define-property',
          'es.object.get-own-property-descriptor',
          'es.object.get-own-property-descriptors',
          'es.object.keys',
          'es.object.to-string',
          'web.dom-collections.for-each',
          'esnext.global-this',
          'esnext.string.match-all',
        ],
      }),
    ],
    resolve: {
      alias: [{ find: '@', replacement: path.resolve(__dirname, 'src') }],
    },
    build: {
      minify: true,
    },
    server: {
      port: 4000,
      proxy: {
        '/xuefu': {
          target: 'https://wtest.xiwang.com/',
          changeOrigin: true,
        },
        //api
      },
    },
  }
})
